<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Finance\GatherInfo;
use App\Models\System\Article;
use App\Models\System\Carousel;
use Illuminate\Http\Request;

class IndexController extends Controller
{

    public function index(Request $request){

        if($request->isMethod('post')){
            $list = Good::where('status',1)
                ->where('is_recommend',1)
                ->select('id','name','images','amount')
                ->skip($page)->take(10)
                ->get()->toArray();

            $total = Good::where('status',1)
                ->where('is_recommend',1)
                ->count('id');
            foreach ($list as $key=>$value){
                $list[$key]['images'] = $value['images'][0];
            }

            return $this->ajaxSuccess(['list'=>$list,'total'=>$total]);
        }else{
            return $this->ajaxError('请求方式有误');
        }

    }


    public function menu(Request $request){

        $menu = $this->redis->get('menu');
        $menu = json_decode($menu,JSON_UNESCAPED_UNICODE);
        return $this->ajaxSuccess($menu);

    }


    public function carousels(Request $request){

        $carousels = Carousel::select('image','url')->where('status',1)->get()->toArray();
        if($carousels){
            return $this->ajaxSuccess($carousels);
        }else{
            return $this->ajaxSuccess([]);
        }

    }



    public function newsList(Request $request){

        $page = $this->decryptData($request->post('page'));
        $page = ($page - 1) * 10;
        $model = Article::where('is_visible',1)
            ->select('id','created_at','title','content')
            ->skip($page)->take(10)->get()->each(function($item){
                $item->makeVisible('summary');
                $item->makeHidden(['content','category_id','updated_at','is_visible','admin_id','good_rate','tags','type']);
            })->toArray();
        $total = Article::where('is_visible',1)->count('id');

        if($model){
            return $this->ajaxSuccess(['model'=>array_values($model),'total'=>$total]);
        }else{
            return $this->ajaxSuccess([]);
        }

    }


    public function detail(Request $request){

        $id = $this->decryptData($request->post('id'));
        $list = Article::where('id',$id)
            ->select('id','created_at','title','content')
            ->first()->toArray();
        if($list){
            return $this->ajaxSuccess($list);
        }else{
            return $this->ajaxError();
        }

    }

}
